package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.biz.ask.AskActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleSmoothActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.biz.main.newdiscover.tuijian.TuijianActivity;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class BaseDiscoverHolder extends RecyclerView.ViewHolder {

    protected TextView commentNum;
    private TextView likeNum;
    protected TextView recommend;
    private ImageView likeNumImg;
    protected View bottom;
    private ImageView work_header;
    private TextView work_name;

    protected View header_Container;

    private RelativeLayout recommendLayout;
    private RelativeLayout commentLayout;
    private RelativeLayout likeLayout;

    private boolean isAnimation_runing;
    private Context mContext;

    protected View mFirstHeaderContainer;
    protected View mFirstFooterContainer;
    protected TextView mFirstHeaderTitle;
    protected TextView mFirstHeaderIntro;
    protected TextView mFirstFooterTitle;

    public BaseDiscoverHolder(View itemView) {
        super(itemView);
        mFirstHeaderContainer = itemView.findViewById(R.id.first_container);
        mFirstFooterContainer = itemView.findViewById(R.id.frist_footer_container);
        mFirstFooterTitle = (TextView) itemView.findViewById(R.id.first_footer_title);
        mFirstHeaderTitle = (TextView) itemView.findViewById(R.id.first_title);
        mFirstHeaderIntro = (TextView) itemView.findViewById(R.id.first_intro);

        header_Container = itemView.findViewById(R.id.ground_head_rly);
        commentNum = (TextView) itemView.findViewById(R.id.ground_commentnumber_tv);
        recommend = (TextView) itemView.findViewById(R.id.ground_recommend_tv);
        likeNum = (TextView) itemView.findViewById(R.id.ground_likenumber_tv);
        likeNumImg = (ImageView) itemView.findViewById(R.id.ground_like_img);
        bottom = itemView.findViewById(R.id.ground_bottom_lly);
        work_header = (ImageView) itemView.findViewById(R.id.ground_work_img);
        work_name = (TextView) itemView.findViewById(R.id.ground_name_tv);

        likeLayout = (RelativeLayout) itemView.findViewById(R.id.ground_like_rly);
        commentLayout = (RelativeLayout) itemView.findViewById(R.id.ground_comment_rly);
        recommendLayout = (RelativeLayout) itemView.findViewById(R.id.ground_recommend_rly);
    }

    public void renderData(final Timeline mItem, final Context mContext) {
        this.mContext = mContext;
        OnClick onClick = new OnClick(mItem);
        if (mItem.isfirst()) {
            mFirstHeaderTitle.setText(mContext.getString(R.string.todayrecommend));
            mFirstHeaderIntro.setText(mContext.getString(R.string.invisible_content));
            mFirstFooterTitle.setText(mContext.getString(R.string.more_recommend));

            mFirstHeaderContainer.setVisibility(View.VISIBLE);
            mFirstFooterContainer.setVisibility(View.VISIBLE);
            if (header_Container != null && bottom != null) {
                header_Container.setVisibility(View.GONE);
                bottom.setVisibility(View.GONE);
            }
        } else {
            mFirstHeaderContainer.setVisibility(View.GONE);
            mFirstFooterContainer.setVisibility(View.GONE);
            if (header_Container != null && bottom != null) {
                header_Container.setVisibility(View.VISIBLE);
                bottom.setVisibility(View.VISIBLE);
            }
        }
        if (StringUtil.notNullEqual(mItem.getTl_type(), "discover")) {
            if (work_header != null) {
                work_name.setText(mContext.getString(R.string.editor_top_choice));
                work_name.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gotoUtil.gotoAct(mContext, TuijianActivity.class);
                    }
                });
            }
            if (work_header != null) {
                work_header.setImageResource(R.drawable.discover_default_recommend);
                work_header.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        gotoUtil.gotoAct(mContext, TuijianActivity.class);
                    }
                });
            }
        } else {
            if ("tag".equals(mItem.getTl_type())) {
                header_Container.setVisibility(View.VISIBLE);
                if (work_name != null) {
                    if (!TextUtils.isEmpty(mItem.getTag_name())) {
                        work_name.setText(mItem.getTag_name());
                    }
                    work_name.setOnClickListener(onClick);
                }
                if (work_header != null) {
                    if (!TextUtils.isEmpty(mItem.getTag_cover())) {
                        ImageLoader.getInstance().displayImage(mItem.getTag_cover(), work_header, ImageOptions.optionHead);
                    }
                    work_header.setOnClickListener(onClick);
                }
            } else if ("work".equals(mItem.getTl_type())) {
                header_Container.setVisibility(View.VISIBLE);
                if (work_name != null) {
                    if (!TextUtils.isEmpty(mItem.getRealName())) {
                        work_name.setText(mItem.getRealName());
                    }
                    work_name.setOnClickListener(onClick);
                }
                if (work_header != null) {
                    if (!TextUtils.isEmpty(mItem.getWork_cover())) {
                        ImageLoader.getInstance().displayImage(mItem.getWork_cover(), work_header, ImageOptions.optionHead);
                    }
                    work_header.setOnClickListener(onClick);
                }
            } else if ("user".equals(mItem.getTl_type())) {
                header_Container.setVisibility(View.GONE);
            }
        }
        if (bottom != null) {
            if (!TextUtils.isEmpty(mItem.getCommentNum()) && commentNum != null) {
                if (Integer.parseInt(mItem.getCommentNum()) > 0) {
                    commentNum.setText(mItem.getCommentNum());
                } else {
                    commentNum.setText(mContext.getString(R.string.comment));
                }
            }
            if (StringUtil.getHaveDingBoolean(mItem)) {
                likeNumImg.setImageResource(R.drawable.like_pink);
                likeNum.setText(mItem.getLikeNum());
            } else {
                likeNumImg.setImageResource(R.drawable.like_grey);
                likeNum.setText(mItem.getLikeNum());
                if (mItem.getLikeNum().equals("0")) {
                    likeNum.setText(mContext.getString(R.string.good));
                }
            }
            if (StringUtil.notNullEqual(mItem.getHaveRecommend(), "true")) {
                recommend.setText(mContext.getString(R.string.all_recommended));
            } else {
                recommend.setText(mContext.getString(R.string.all_recommend));
            }
            likeLayout.setOnClickListener(onClick);
            commentLayout.setOnClickListener(onClick);
            recommendLayout.setOnClickListener(onClick);
        }
        mFirstFooterContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, TuijianActivity.class);
            }
        });
    }

    private class OnClick implements View.OnClickListener {
        Timeline mItem;
        int timelineType;

        public OnClick(Timeline mItem) {
            this.mItem = mItem;
            timelineType = TimelineFactory.whichType(mItem);
        }

        @Override
        public void onClick(final View v) {
            switch (v.getId()) {
                case R.id.ground_work_img:
                case R.id.ground_name_tv:
                    if (StringUtil.notNullEqual(mItem.getTl_type(), "work")) {
                        gotoUtil.gotoAct(mContext, CircleSmoothActivity.class, mItem.getRealName(), mItem.getDetailWid());
                    } else if (StringUtil.notNullEqual(mItem.getTl_type(), "tag")) {
                        Intent intent = new Intent(mContext, CircleTagSmoothActivity.class);
                        intent.putExtra("type", "tag");
                        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, mItem.getTag_name());
                        mContext.startActivity(intent);
                    }
                    break;
                case R.id.ground_comment_rly:
                    switch (timelineType) {
                        case TimelineFactory.TIMELINE_TYPE_COSER:
                            DetailDataHelper.gotoCosDetail(mContext, mItem.getRp_id(), mItem.getCp_id(), true);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_ILLUST:
                            DetailDataHelper.gotoDrawerDetail(mContext, mItem.getRp_id(), mItem.getDp_id(), true);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_DAILY:
                            DetailDataHelper.gotoDailyDetail(mContext, mItem.getUd_id(), true);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_GROUP:
                            DetailDataHelper.gotoGroupDetail(mContext, mItem.getGid(), mItem.getPost_id(), true);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_WRITER:
                            DetailDataHelper.gotoWriterDetail(mContext, mItem.getRp_id(), mItem.getWp_id(), true);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_ASK:
                            gotoUtil.gotoAct(mContext, AskActivity.class, mItem.getOuid(), mItem.getOUName());
                            break;
                        case TimelineFactory.TIMELINE_TYPE_GOODS:
                            gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, mItem.getZg_id());
                            break;
                    }
                    break;
                case R.id.ground_recommend_rly:
                    TimelineLikeHelper.doRecommendAll(mContext, VolleyQueue.getRquest(mContext), mItem, new TimelineLikeHelper.TimelineSuccess() {
                        @Override
                        public void onTimelineSuccess() {
                            mItem.setHaveRecommend(String.valueOf(!Boolean.valueOf(mItem.getHaveRecommend())));
                            renderRecommend(v, mItem);
                        }
                    });
                    break;
                case R.id.ground_like_rly:
                    if (isAnimation_runing) break;
                    TimelineLikeHelper.doLikeAll(mContext, VolleyQueue.getRquest(mContext), mItem, new TimelineLikeHelper.TimelineSuccess() {
                        @Override
                        public void onTimelineSuccess() {
                            if (!Boolean.parseBoolean(mItem.getHaveDing())) {
                                mItem.setHaveDing(String.valueOf(true));
                                mItem.setLikeNum(StringUtil.getString(
                                        StringUtil.getInt(mItem.getLikeNum()) + 1));
                            } else {
                                mItem.setHaveDing(String.valueOf(false));
                                mItem.setLikeNum(StringUtil.getString(
                                        StringUtil.getInt(mItem.getLikeNum()) - 1));
                            }
                            renderPaise(v, mItem);
                        }
                    });
                    break;
            }
        }
    }

    private void renderPaise(View view, final Timeline mItem) {
        final ImageView iv = (ImageView) view.findViewById(R.id.ground_like_img);
        final TextView tv = (TextView) view.findViewById(R.id.ground_likenumber_tv);
        if (StringUtil.getHaveDingBoolean(mItem)) {
            isAnimation_runing = true;
            iv.setScaleY(2f);
            iv.setScaleX(2f);
            iv.setImageResource(R.drawable.frame_praise);
            AnimationDrawable drawable = (AnimationDrawable) iv.getDrawable();
            drawable.start();
            iv.postDelayed(new Runnable() {
                @Override
                public void run() {
                    iv.setScaleY(1f);
                    iv.setScaleX(1f);
                    iv.setImageResource(R.drawable.like_pink);
                    tv.setText(mItem.getLikeNum());
                    isAnimation_runing = false;
                }
            }, 24 * 20);
        } else {
            iv.setImageResource(R.drawable.like_grey);
            tv.setText(mItem.getLikeNum());
            if (mItem.getLikeNum().equals("0")) {
                tv.setText(mContext.getString(R.string.good));
            }
        }
    }

    private void renderRecommend(View view, Timeline mItem) {
        TextView tv = (TextView) view.findViewById(R.id.ground_recommend_tv);
        if (StringUtil.notNullEqual(mItem.getHaveRecommend(), "true")) {
            tv.setText(mContext.getString(R.string.all_recommended));
        } else {
            tv.setText(mContext.getString(R.string.all_recommend));
        }
    }


}
